// 所有模块都通过 define 来定义
define(function(require, exports, module) {

	// 通过 require 引入依赖
	require('jquery');
	require('./css/upload.css');
	require('./src/default.css');
	require('./src/kindeditor-min.js');
	require.async('./src/lang/zh_CN.min.js');
	require('layer');
	
	//var layer = require('layer');
	
	/**
	 * config = {appid:'',appkey:''}
	 */
	exports.run = function(config){
		var uploadurl = '/material/image/upload';
		var editor = KindEditor.editor({
			uploadJson : uploadurl,
			allowFileManager : true,
			fileManagerJson : config.fileManagerJson,
		});
		
		$('#uploadimg_m').click(function() {
			editor.loadPlugin('multiimage', function() {
				editor.plugin.multiImageDialog({
					clickFn : function(urlList) {
						var div = $('#all-pics');
						$.each(urlList, function(i, data) {
							//data.id = i; //demo,实际应用中删除
							if(!$('.gpic-box span.current').html() && i == 0){
								var html ='<div class="gpic-box" id="up_'+data.id+'">' 
								+'<span data-id="'+data.id+'" class="current tplr"><img src="' + data.url + '" width="46" height="50" style="margin-top: 1px;"></span>' 
								+'<div class="gpic-btn-bar">'  
								+'<span class="gpic-btn-view show_img_info" data-id="'+data.id+'" title="查看路径"><i>查看路径</i></span>|<span class="gpic-btn-delete" data-id="'+data.id+'" title="删除图片"><i>删除图片</i></span>' 
								+'</div>'
								+'<div style="display:none" id="img_detail_'+data.id+'">'
								+'<div class="img_detail">'
								+'<input type="hidden" name="img_list_id[]" value="'+data.id+'" />'
								+'<label>图片标题： <input type="text" name="img_list_title[]" class="img_title" value="" /></label><br><br>'
								+'<label>替代文字： <input type="text" name="img_list_alt[]" class="img_alt" value="" /></label><br><br>'
								+'<label>跳转链接： <input type="text" name="img_list_redurl[]" class="img_redurl" value="" /></label><br><br>'
								+'<label>跳转方式： <select name="img_list_target[]" class="img_target"><option value="_self">直接跳转</option><option value="_blank">打开新窗口</option></select></lable>'
								+'</div>'
								+'</div>'
								+'</div>';
								
								$('#img_id').val(data.id);
								$('#spic_img').html('<img src="'+data.url+'"/>');
							}else{
								var html ='<div class="gpic-box" id="up_'+data.id+'">' 
								+'<span data-id="'+data.id+'" class="tplr"><img src="' + data.url + '" width="46" height="50" style="margin-top: 1px;"></span>' 
								+'<div class="gpic-btn-bar">'  
								+'<span class="gpic-btn-view show_img_info" data-id="'+data.id+'" title="查看路径"><i>查看路径</i></span>|<span class="gpic-btn-delete" data-id="'+data.id+'" title="删除图片"><i>删除图片</i></span>' 
								+'</div>'  
								+'<div style="display:none" id="img_detail_'+data.id+'">'
								+'<div class="img_detail">'
								+'<input type="hidden" name="img_list_id[]" value="'+data.id+'" />'
								+'<label>图片标题： <input type="text" name="img_list_title[]" class="img_title" value="" /></label><br><br>'
								+'<label>替代文字： <input type="text" name="img_list_alt[]" class="img_alt" value="" /></label><br><br>'
								+'<label>跳转链接： <input type="text" name="img_list_redurl[]" class="img_redurl" value="" /></lable><br><br>'
								+'<label>跳转方式： <select name="img_list_target[]" class="img_target"><option value="_self">直接跳转</option><option value="_blank">打开新窗口</option></select></lable>'
								+'</div>'
								+'</div>'
								+'</div>';
							}
							div.append(html);
						});
						editor.hideDialog();
						
						doing(config); //内部函数调用，往新的dom中注入点击事件
					}
				});
			});
		});
		
		function doing(config){
			$('.gpic-btn-delete').click(function(){
				var $this = $(this);
				var imgid = $this.attr('data-id');
				var asker = layer.confirm('是否确定删除?', function(){
					divid = "#up_"+imgid;
					var delurl = '/material/image/del';
					$.get(delurl+'/ids/'+imgid,function(data){
						if(data['status'] == 1){
							$(divid).remove();
							if($('#img_id').val() == imgid){
								$('#img_id').val('');
								$('#spic_img').html('');
							}
						}else{
							alert('图片无法删除');
						}
					},'json');
					
					
					layer.close(asker);
				});
			});
			
			$('.tplr').click(function(){
				var $this = $(this);
				imgid = $this.attr('data-id');
				var divid = "#up_"+imgid;
				var img_src = $("#up_"+imgid+" > span > img").attr('src');
				$('#img_id').val(imgid);
				$(".gpic-box > span").removeClass('current');
				$("#up_"+imgid+" > span").eq(0).addClass('current');
				$('#spic_img').html('<img src="'+img_src+'"/>');
			});
			
			$('.show_img_info').click(function(){
				var id = 'img_detail_'+$(this).attr('data-id');
				layer.open({
				    type: 1,
				    maxmin: true,
				    title: '图片属性',
				    area: ['400px', '300px'],
				    content:$('#'+id),
					btns: 2,
				    btn: ['确定','关闭'],
				    yes: function(index){
			            var title = $('#xubox_layer'+index+' .img_detail .img_title').val();
			            var alt = $('#xubox_layer'+index+' .img_detail .img_alt').val();
			            $('#'+id+' .img_detail .img_title').attr('value',title);
			            $('#'+id+' .img_detail .img_alt').attr('value',alt);
			            layer.close(index);
			        },
			        no: function(index){
			            layer.close(index);
			        }
				});
			});
		}
		
		doing(config); //对当前的dom注入点击事件
		
	}
	
});